//
//  TGDealWebController.m
//  GroupBuy
//
//  Created by wwj on 15/11/12.
//  Copyright © 2015年 wwj. All rights reserved.
//

#import "TGDealWebController.h"
#import "TGDealModel.h"

@interface TGDealWebController ()<UIWebViewDelegate>
@property (strong, nonatomic) UIWebView *webView;
@property (assign, nonatomic) BOOL flag;
@end

@implementation TGDealWebController

-(void)setModel:(TGDealModel *)model{
    _model = model;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = kBgColor;
    
    _webView = [[UIWebView alloc] initWithFrame:[UIScreen mainScreen].bounds];
    _webView.autoresizingMask = UIViewAutoresizingFlexibleHeight | UIViewAutoresizingFlexibleWidth;
    _webView.delegate = self;
    _webView.backgroundColor = kBgColor;
    self.view = _webView;
    
    NSString *ID = [_model.deal_id substringFromIndex:[_model.deal_id rangeOfString:@"-"].location +1];
    
    NSString *urlStr = [NSString stringWithFormat:@"http://m.dianping.com/tuan/deal/moreinfo/%@",ID];
    
    NSURL *url = [NSURL URLWithString:urlStr];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [_webView loadRequest:request];
    
    
}

-(void)webViewDidStartLoad:(UIWebView *)webView{
    [MBProgressHUD showMessage:@"加载中..." toView:_webView];
}

-(void)webViewDidFinishLoad:(UIWebView *)webView{
    [MBProgressHUD hideAllHUDsForView:_webView animated:YES];
    webView.scrollView.contentInset = UIEdgeInsetsMake(70, 0, 0, 0);
    if (_flag) {
        [webView stringByEvaluatingJavaScriptFromString:[self getScriptStr]];
        _flag = FALSE;
    }
    _flag = TRUE;
}

-(NSString *)getScriptStr{
    NSMutableString *script = [NSMutableString string];
    [script appendString:@"var body = document.body;"];
    [script appendString:@"var section  = document.getElementsByTagName('link')[5];"];
    [script appendString:@"var section1 = document.getElementsByTagName('div')[8];"];
    [script appendString:@"var section2 = document.getElementsByTagName('div')[14];"];
    [script appendString:@"var section3 = document.getElementsByTagName('div')[19];"];
    [script appendString:@"var section4 = document.getElementsByTagName('div')[32];"];
    [script appendString:@"body.innerHTML = '';"];
    [script appendString:@"body.appendChild(section);"];
    [script appendString:@"body.appendChild(section1);"];
    [script appendString:@"body.appendChild(section2);"];
    [script appendString:@"body.appendChild(section3);"];
    [script appendString:@"body.appendChild(section4);"];
    
    [script appendString:@"var images= document.getElementsByTagName('img');"];
    [script appendString:@"for(var i=0;i<images.length;i++){"];
    [script appendString:[NSString stringWithFormat:@"images[i].width=%f;", self.view.frame.size.width]];
    [script appendString:@"images[i].height=340;"];
    [script appendString:@"}"];
    return script;
}



@end
